package com.ym.jiujiangeyou;

import java.util.List;

import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.app.MyApplication;
import com.ym.jiujiangeyou.bitmap.BitmapUtil;
import com.ym.jiujiangeyou.bitmap.SimpleImageLoad;
import com.ym.jiujiangeyou.database.HotelService;
import com.ym.jiujiangeyou.database.SceneryService;
import com.ym.jiujiangeyou.entity.Hotel;
import com.ym.jiujiangeyou.service.PicUrlService;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;
import com.ym.jiujiangeyou.util.FileManager;
import com.ym.jiujiangeyou.util.Util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class HotelDetails extends Activity implements OnClickListener {

	private TextView title, h_phone, h_address;
	private RelativeLayout rel_introduct, rel_traffic, rel_facility, rel_phone,
			rel_address;
	private LinearLayout hotel_linearlayout;
	private Hotel hotel = null;
	private int width;
	private int height;
	ImageView img_collect, hotel_pic;
	Dialog dialog;
	Toast toast;
	SimpleImageLoad simpleImageLoad;
	private LinearLayout title_back;
	MyApplication application = null;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case Constants.SCENERY_COLLECT_SUCCESS:
				img_collect.setVisibility(View.GONE);
				img_collect.setClickable(false);
				dialog.dismiss();
				toast = DialogView.popToast(HotelDetails.this,
						R.string.detail_collect_success);
				toast.show();
				break;
			case Constants.SCENERY_COLLECT_ERROR:
				dialog.dismiss();
				toast = DialogView.popToast(HotelDetails.this,
						R.string.detail_collect_error);
				toast.show();
				break;
			case Constants.SCENERY_COLLECT_ALREADY:
				img_collect.setVisibility(View.GONE);
				img_collect.setClickable(false);
				break;
			case Constants.SCENERY_COLLECT_NOT:
				img_collect.setVisibility(View.VISIBLE);
				img_collect.setOnClickListener(HotelDetails.this);
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		application = MyApplication.getInstance();
		super.setContentView(R.layout.hotel_details);

		hotel = getData();

		/**
		 * 启动service获取图片连接
		 */
		Intent intent2 = new Intent(this, PicUrlService.class);
		intent2.putExtra("id", hotel.getId());
		intent2.putExtra("flag", "hotel");
		intent2.putExtra("url", Constants.GET_HOTEL_PIC_URL);
		startService(intent2);

		init();

	}

	public Hotel getData() {
		// Hotel hotel = new Hotel() ;
		Bundle bundle = getIntent().getBundleExtra("hotel");
		Hotel hotel = new Hotel(bundle.getInt("id"), bundle.getString("name"),
				bundle.getString("address"), bundle.getString("introduction"),
				bundle.getString("phone"), bundle.getDouble("longitude"),
				bundle.getDouble("latitude"), bundle.getString("facility"),
				bundle.getString("imageurl"), bundle.getString("traffic"),
				bundle.getString("price"), bundle.getString("priceinfo"));
		return hotel;
	}

	public void init() {
		title = (TextView) this.findViewById(R.id.title_text);
		h_phone = (TextView) findViewById(R.id.hotel_phone);
		h_address = (TextView) findViewById(R.id.hotel_address);
		title_back = (LinearLayout) this.findViewById(R.id.title_back);

		rel_introduct = (RelativeLayout) this
				.findViewById(R.id.rel_hotel_introduct);
		rel_traffic = (RelativeLayout) this
				.findViewById(R.id.rel_hotel_traffic);
		rel_facility = (RelativeLayout) this
				.findViewById(R.id.rel_hotel_facility);
		rel_address = (RelativeLayout) this
				.findViewById(R.id.rel_hotel_address);
		rel_phone = (RelativeLayout) findViewById(R.id.rel_hotel_phone);

		hotel_linearlayout = (LinearLayout) findViewById(R.id.hotel_linearlayout);
		img_collect = (ImageView) findViewById(R.id.img_collect);
		hotel_pic = (ImageView) findViewById(R.id.hotel_pic);

		if (hotel.getImageurl() != null
				&& FileManager.getBitmap(hotel.getImageurl()) != null) {
			hotel_pic
					.setImageBitmap(FileManager.getBitmap(hotel.getImageurl()));
		} else {
			simpleImageLoad = new SimpleImageLoad(this);
			simpleImageLoad.DisplayImage(hotel.getImageurl(), hotel_pic);
		}
		isCollectImgShow();

		// 等到屏幕的大小
		/*
		 * width = this.getWindowManager().getDefaultDisplay().getWidth();
		 * height = this.getWindowManager().getDefaultDisplay().getHeight();
		 * height = (height / 5) * 2; LinearLayout.LayoutParams layoutParams =
		 * (LinearLayout.LayoutParams) hotel_linearlayout .getLayoutParams();
		 * layoutParams.width = width; layoutParams.height = height;
		 * hotel_linearlayout.setLayoutParams(layoutParams);
		 * LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
		 * LinearLayout.LayoutParams.WRAP_CONTENT,
		 * LinearLayout.LayoutParams.WRAP_CONTENT); ImageView iv = new
		 * ImageView(this); iv.setLayoutParams(mParams);
		 * iv.setImageBitmap(BitmapUtil.zoomImage(
		 * BitmapFactory.decodeResource(getResources(), R.drawable.sd), width,
		 * height)); hotel_linearlayout.addView(iv);
		 */

		title.setText(hotel.getName());
		h_phone.setText(hotel.getPhone());
		h_address.setText(hotel.getAddress());

		rel_introduct.setOnClickListener(this);
		rel_traffic.setOnClickListener(this);
		rel_facility.setOnClickListener(this);
		rel_address.setOnClickListener(this);
		rel_phone.setOnClickListener(this);
		title_back.setOnClickListener(this);
		hotel_pic.setOnClickListener(this);
	}

	/*
	 * @retur void
	 * 
	 * @Description: 查询是否搜藏过
	 */
	public void isCollectImgShow() {
		new Thread(new Runnable() {
			Message m = new Message();
			HotelService h = new HotelService(HotelDetails.this);

			@Override
			public void run() {
				// TODO Auto-generated method stub
				boolean flag = h.queryByName(hotel.getName());
				if (!flag) {
					m.what = Constants.SCENERY_COLLECT_NOT;
				} else {
					m.what = Constants.SCENERY_COLLECT_ALREADY;
				}
				handler.sendMessage(m);
			}
		}).start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.hotel_phone:
			intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ hotel.getPhone()));
			startActivity(intent);
			break;
		case R.id.rel_hotel_introduct:
			intent = new Intent(this, HotelIntroduct.class);
			intent.putExtra("name", hotel.getName());
			intent.putExtra("introduct", hotel.getIntroduction());
			startActivity(intent);

			break;
		case R.id.rel_hotel_traffic:
			intent = new Intent(this, HotelTraffic.class);
			intent.putExtra("name", hotel.getName());
			intent.putExtra("traffic", hotel.getTraffic());
			startActivity(intent);
			break;
		case R.id.rel_hotel_facility:
			intent = new Intent(this, HotelFacility.class);
			intent.putExtra("name", hotel.getName());
			intent.putExtra("facility", hotel.getFacility());
			startActivity(intent);
			break;
		case R.id.rel_hotel_address:
			intent = new Intent(HotelDetails.this, SceneryMapActivity.class);
			Bundle bundle = new Bundle();
			bundle.putDouble("hLat", hotel.getLatitude());
			bundle.putDouble("hLon", hotel.getLongitude());
			bundle.putString("hName", hotel.getName());
			bundle.putString("flag", "hotel");
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case R.id.rel_hotel_phone:
			intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ hotel.getPhone()));
			startActivity(intent);
			break;
		case R.id.img_collect:
			dialog = DialogView
					.popDialog(this, R.string.detail_collect_loading);
			dialog.show();
			new Thread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Message message = new Message();
					HotelService hotelService = new HotelService(
							HotelDetails.this);
					long i = hotelService.insetHotelToBase(hotel);
					if (i > 0) {
						message.what = Constants.SCENERY_COLLECT_SUCCESS;
					} else {
						message.what = Constants.SCENERY_COLLECT_ERROR;
					}
					handler.sendMessage(message);
				}
			}).start();
			break;
		case R.id.hotel_pic:
			// List<String> urls = application.getUrls() ;
			List<String> urls = application.getHotelUrlsCache(Util
					.replaceUrlWithPlus(Constants.GET_HOTEL_PIC_URL
							+ hotel.getId()));
			// System.out.println("-------------urls---------------"+urls.size())
			// ;
			if (urls != null && !urls.isEmpty()) {
				application.setUrls(urls);
				intent = new Intent(HotelDetails.this,
						PictureBrowerActivity.class);
				startActivity(intent);
			} else {
				if (hotel.getImageurl() != null
						&& FileManager.getBitmap(hotel.getImageurl()) != null) {
					intent = new Intent(HotelDetails.this,
							PicDetailActivity.class);
					intent.putExtra("imageurl", hotel.getImageurl());
					startActivity(intent);
				} else {
					Toast.makeText(this, R.string.http_connect_error,
							Toast.LENGTH_SHORT).show();
				}

			}

			break;
		default:
			break;
		}
	}

}
